package mh;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

/**
 * WARNING TEST CLASS!!!!!
 * 
 * @author gianmarcolaggia
 * 
 */
public class ClientUDP implements Runnable {
	private final MagicHogwarts	game;

	public ClientUDP(MagicHogwarts g) {
		this.game = g;
	}

	@Override
	public void run() {
		MulticastSocket sock;
		byte[] buffer = new byte[65535];
		try {
			sock = new MulticastSocket(5000);

			int port = 5000;
			InetAddress addr = InetAddress.getByName("235.0.0.1");
			sock.joinGroup(addr);
			while (true) {
				DatagramPacket packet = new DatagramPacket(buffer, buffer.length, addr, port);
				sock.receive(packet);
				String address = packet.getAddress().toString();
				InetAddress client = packet.getAddress();
				int client_port = packet.getPort();

				//				try {
				//					this.game.enqueueMessage(MessageParser.parseMessage(new String(buffer).trim()));
				//				} catch (ParseException e) {
				//
				//					e.printStackTrace();
				//				}

				System.out.println("Received : '" + new String(buffer).trim() + "' from " + address + "\n");
				// String message = "RECEIVED";
				// buffer = message.getBytes();
				// packet = new DatagramPacket(buffer, buffer.length, addr, 5000);
				// s1.send(packet);
			}
		} catch (IOException e) {
			// DONOTHING
		}

	}
}
// import java.awt.Checkbox;
// import java.awt.event.ActionEvent;
// import java.awt.event.ActionListener;
// import java.io.IOException;
// import java.net.DatagramPacket;
// import java.net.InetAddress;
// import java.net.MulticastSocket;
//
// import javax.swing.JButton;
// import javax.swing.JFrame;
// import javax.swing.JLabel;
// import javax.swing.JList;
// import javax.swing.JPanel;
// import javax.swing.JRootPane;
// import javax.swing.JScrollPane;
// import javax.swing.JTextArea;
// import javax.swing.JTextField;
//
// public class UDPMulticastClient {
// JFrame frame;
// JPanel panel;
// JTextField field1, field2, field3, field4, field5, field6, field7, field8;
// JTextArea area;
// JScrollPane pane;
// JLabel label;
// JButton button;
// JList list;
// Checkbox check1, check2, check3, check4;
// MulticastSocket s1, s2, s3;
//
// public static void main(String[] args) {
// UDPMulticastClient u = new UDPMulticastClient();
// }
//
// public UDPMulticastClient() {
// try {
// this.s1 = new MulticastSocket();
// this.s2 = new MulticastSocket();
// this.s3 = new MulticastSocket();
// } catch (IOException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }
// this.frame = new JFrame("UDP Broadcast Client");
// this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// this.frame.setUndecorated(true);
// this.frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
// this.panel = new JPanel();
// this.panel.setLayout(null);
// this.label = new JLabel("Destination IP");
// this.label.setBounds(80, 5, 100, 30);
// this.panel.add(this.label);
// this.check1 = new Checkbox();
// this.check1.setBounds(5, 40, 20, 20);
// this.panel.add(this.check1);
// this.label = new JLabel("Client 1 :");
// this.label.setBounds(25, 35, 60, 30);
// this.panel.add(this.label);
// this.field1 = new JTextField(20);
// this.field1.setBounds(80, 35, 120, 20);
// this.panel.add(this.field1);
// this.label = new JLabel("Destination Port");
// this.label.setBounds(255, 5, 100, 30);
// this.panel.add(this.label);
// this.field2 = new JTextField(10);
// this.field2.setBounds(255, 35, 100, 20);
// this.panel.add(this.field2);
// this.check2 = new Checkbox();
// this.check2.setBounds(5, 70, 20, 20);
// this.panel.add(this.check2);
// this.label = new JLabel("Client 2 :");
// this.label.setBounds(25, 65, 60, 30);
// this.panel.add(this.label);
// this.field3 = new JTextField(20);
// this.field3.setBounds(80, 65, 120, 20);
// this.panel.add(this.field3);
// this.field4 = new JTextField(10);
// this.field4.setBounds(255, 65, 100, 20);
// this.panel.add(this.field4);
// this.check3 = new Checkbox();
// this.check3.setBounds(5, 100, 20, 20);
// this.panel.add(this.check3);
// this.label = new JLabel("Client 3 :");
// this.label.setBounds(25, 95, 60, 30);
// this.panel.add(this.label);
// this.field5 = new JTextField(20);
// this.field5.setBounds(80, 95, 120, 20);
// this.panel.add(this.field5);
// this.field6 = new JTextField(10);
// this.field6.setBounds(255, 95, 100, 20);
// this.panel.add(this.field6);
// this.check4 = new Checkbox();
// this.check4.setBounds(5, 130, 20, 20);
// this.panel.add(this.check4);
// this.label = new JLabel("Client 4 :");
// this.label.setBounds(25, 125, 60, 30);
// this.panel.add(this.label);
// this.field7 = new JTextField(20);
// this.field7.setBounds(80, 125, 120, 20);
// this.panel.add(this.field7);
// this.field8 = new JTextField(10);
// this.field8.setBounds(255, 125, 100, 20);
// this.panel.add(this.field8);
// this.label = new JLabel("Message:");
// this.label.setBounds(10, 160, 80, 30);
// this.panel.add(this.label);
// this.area = new JTextArea();
// this.pane = new JScrollPane(this.area);
// this.pane.setBounds(10, 190, 300, 200);
// this.panel.add(this.pane);
// this.button = new JButton("Send");
// this.button.setBounds(235, 410, 75, 30);
// this.button.addActionListener(new ActionListener() {
// @Override
// public void actionPerformed(ActionEvent e) {
// new SendRequest();
// }
// });
// this.panel.add(this.button);
// this.frame.add(this.panel);
// this.frame.setSize(400, 500);
// this.frame.setVisible(true);
// }
//
// public class SendRequest {
// SendRequest() {
// try {
// boolean b = true;
// if (UDPMulticastClient.this.check1.getState() == b) {
// String dip = UDPMulticastClient.this.field1.getText();
// InetAddress address = InetAddress.getByName(dip);
// // MulticastSocket socket = new MulticastSocket();
// UDPMulticastClient.this.s1.joinGroup(address);
// UDPMulticastClient.this.s2.joinGroup(address);
// UDPMulticastClient.this.s3.joinGroup(address);
// String port = UDPMulticastClient.this.field2.getText();
// int pnum = Integer.parseInt(port);
// String mess = UDPMulticastClient.this.area.getText();
// byte message[] = mess.getBytes();
// DatagramPacket packet = new DatagramPacket
//
// (message, message.length, address, pnum);
// UDPMulticastClient.this.s1.send(packet);
// UDPMulticastClient.this.area.setText("");
// // For Received message
// DatagramPacket packet1 = new DatagramPacket
//
// (message, message.length);
// UDPMulticastClient.this.s1.receive(packet1);
// String recmessage = new String(packet1.getData());
// UDPMulticastClient.this.area.append("Received from server: " + recmessage);
// UDPMulticastClient.this.s1.close();
// }
// if (UDPMulticastClient.this.check2.getState() == b) {
// String dip = UDPMulticastClient.this.field3.getText();
// InetAddress address = InetAddress.getByName(dip);
// // MulticastSocket socket = new MulticastSocket();
// // s2.joinGroup(address);
// String port = UDPMulticastClient.this.field4.getText();
// int pnum = Integer.parseInt(port);
// String mess = UDPMulticastClient.this.area.getText();
// byte message[] = mess.getBytes();
// DatagramPacket packet = new DatagramPacket(message, message.length, address, pnum);
// // socket.send(packet);
// // UDPMulticastClient.this.area.setText("");
// // For Received message
// DatagramPacket packet1 = new DatagramPacket(message, message.length);
// System.out.println("noproblem ntil here");
// try {
// UDPMulticastClient.this.s2.receive(packet1);
// String recmessage = new String(packet1.getData());
// UDPMulticastClient.this.area.append("Received from server: " + recmessage);
// UDPMulticastClient.this.s2.close();
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// if (UDPMulticastClient.this.check3.getState() == b) {
// String dip = UDPMulticastClient.this.field5.getText();
// InetAddress address = InetAddress.getByName(dip);
// MulticastSocket socket = new MulticastSocket();
// socket.joinGroup(address);
// String port = UDPMulticastClient.this.field6.getText();
// int pnum = Integer.parseInt(port);
// String mess = UDPMulticastClient.this.area.getText();
// byte message[] = mess.getBytes();
// DatagramPacket packet = new DatagramPacket
//
// (message, message.length, address, pnum);
// // socket.send(packet);
// // UDPMulticastClient.this.area.setText("");
// // For Received message
// DatagramPacket packet1 = new DatagramPacket(message, message.length);
// socket.receive(packet1);
// String recmessage = new String(packet1.getData());
// UDPMulticastClient.this.area.append("Received from server: " + recmessage);
// socket.close();
// }
// if (UDPMulticastClient.this.check4.getState() == b) {
// String dip = UDPMulticastClient.this.field7.getText();
// InetAddress address = InetAddress.getByName(dip);
// MulticastSocket socket = new MulticastSocket();
// socket.joinGroup(address);
// String port = UDPMulticastClient.this.field8.getText();
// int pnum = Integer.parseInt(port);
// String mess = UDPMulticastClient.this.area.getText();
// byte message[] = mess.getBytes();
// DatagramPacket packet = new DatagramPacket
//
// (message, message.length, address, pnum);
// // socket.send(packet);
// // UDPMulticastClient.this.area.setText("");
// // For Received message
// DatagramPacket packet1 = new DatagramPacket(message, message.length);
// socket.receive(packet1);
// String recmessage = new String(packet1.getData());
// UDPMulticastClient.this.area.append("Received from server: " + recmessage);
// socket.close();
// }
// } catch (IOException io) {
// }
// }
// }
// }